package com.hnuit.dao;

import com.hnuit.pojo.Courses;
import com.hnuit.pojo.Users;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface Usermapper {
    @Select("select * from users where id = #{id}")
    Users selectusers(int id);

    @Insert("insert into users(id,username,password,email)"
            + "values(#{id},#{username},#{password},#{email})")
    int insertusers(Users users);

    @Update("update users set username = #{username},password = #{password},email= #{email}"
            + "where id = #{id}")
    int updateusers(Users users);

    @Delete("delete from users where id = #{id}")
    int deleteusers(int id);


    @Select("SELECT u.id AS user_id, u.username, u.password, u.email " +
            "FROM users u " +
            "WHERE u.id = #{id}")
    @Results({
            @Result(property = "id", column = "user_id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "email", column = "email"),
            @Result(property = "courses",
                    javaType = List.class,
                    column = "user_id",
                    many = @Many(select = "com.hnuit.dao.CoursesMapper.selectCoursesByInstructorId"))
    })
    Users selectInstructorWithCourses(Integer id);
    @Select("SELECT u.id AS user_id, u.username, u.email " +
            "FROM users u "
            )
    @Results({
            @Result(property = "id", column = "user_id"),
            @Result(property = "username", column = "username"),
            @Result(property = "email", column = "email"),
            @Result(property = "courses",
                    javaType = List.class,
                    column = "user_id",
                    many = @Many(select = "com.hnuit.dao.CoursesMapper.selectCoursesByUserId"))
    })
    List<Users> selectAllUsersWithCourses();
}






